package com.zl.learning.thread;

import java.util.concurrent.ThreadPoolExecutor;

import static java.util.concurrent.Executors.newFixedThreadPool;

public class TestThreadPool implements Runnable{
    private ThreadPoolExecutor executor = init(10);

    public static final TestThreadPool test = new TestThreadPool();

    private ThreadPoolExecutor init(int poolSize){
        return (ThreadPoolExecutor) newFixedThreadPool(poolSize);
    }

    public static void main(String[] args) {
        new Thread(test).start();
    }

    @Override
    public void run() {
        for(int i=0;i<10;i++){
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程池中线程数目："+executor.getPoolSize()+
                    "，队列中等待执行的任务数目："+executor.getQueue().size()+
                    "，taskCount："+executor.getTaskCount()+
                    "，已执行玩别的任务数目："+executor.getCompletedTaskCount()+
                    "，核心数大小："+executor.getCorePoolSize()+
                    "，线程池最大线程数："+executor.getMaximumPoolSize()+
                    "，线程池最大线程数2："+executor.getLargestPoolSize()+
                    "，活动中的线程："+executor.getActiveCount());
            executor.execute(new MyTask(i));
            executor.execute(new MyTask(100+i));
            executor.execute(new MyTask(1000+i));
        }
        executor.shutdown();
    }

    static class MyTask implements Runnable {
        private int taskNum;

        public MyTask(int num) {
            this.taskNum = num;
        }

        @Override
        public void run() {
            System.out.println("正在执行task "+taskNum);
            try {
                Thread.sleep(400);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("task "+taskNum+"执行完毕");
        }
    }
}
